#include "customapplication.h"
#include <QEventLoop>
#include <QThread>
#include <QTimer>

void CustomApplication::Init_customapplication()
{
    // 显示登录窗口
    w.show();

    // 创建线程和任务对象
    m_worker = new ObjectWorkerThread();  // 创建任务对象
    m_thread = new QThread(this);             // 创建线程

    // 将任务对象移入线程
    m_worker->moveToThread(m_thread);

    // 配置信号和槽
    connect(m_thread, &QThread::started, m_worker, &ObjectWorkerThread::process);
    connect(m_worker, &ObjectWorkerThread::dataReady, [&](const QStringList &times,
                                                          const QStringList &currents,
                                                          const QStringList &voltages,
                                                          const QStringList &powers) {
        // 数据接收后存储
        m_times = times;
        m_currents = currents;
        m_voltages = voltages;
        m_powers = powers;

        // 调试打印
        qDebug() << "数据已接收并存储到CustomApplication中";
        qDebug() << "时间列表:" << m_times;
        qDebug() << "电流列表:" << m_currents;
        qDebug() << "电压列表:" << m_voltages;
        qDebug() << "功率列表:" << m_powers;

        // 发送数据到 LineChart
        //emit sendData(m_times, m_currents, m_voltages, m_powers);
    });

    // 确保线程结束时清理任务对象
    connect(m_thread, &QThread::finished, m_worker, &ObjectWorkerThread::deleteLater);

    // 启动线程
    m_thread->start();

    // 登录窗口触发进入主窗口的逻辑
    connect(&w, &LoginForm::start_item, [&]() {
        w.close();  // 关闭登录窗口
        mainWindow.show();  // 显示主窗口
    });
    connect(&w, &LoginForm::abnormal_end, [&]() {
        qDebug()<<"请重新输入账号或者密码";
    });

    // 安装事件过滤器以监听按钮事件
    if (w.ui->signinpushButton) {
        w.ui->signinpushButton->installEventFilter(&w);
    } else {
        qDebug() << "signinpushButton 事件未初始化。";
    }


}
